<!--
@name:dialogProjectConfiguration
@author:Zhenglei
@date:15:40
@description:dialogProjectConfiguration
@update:2022/8/1515:40
-->
<template>
  <el-dialog :title="title"
             :visible.sync="visible"
             :before-close="modalClose"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="60%">
    <el-form :model="subData"
             label-position="right"
             label-width="150px"
             ref="formModelRef"
             :rules='formModelRules'>
      <el-form-item label="常测项名称：" prop="name">
        <el-input v-model="subData.name" placeholder="请输入常测项名称" class="w-300" clearable/>
      </el-form-item>

      <el-form-item label="主题：" prop="dolThemeId">
        <el-select v-model="subData.dolThemeId"
                   filterable
                   class="w-300"
                   placeholder="请选择主题">
          <el-option v-for="item in themeDataSources"
                     :key="item.id"
                     :label="item.dolThemeName"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序：" prop="sort">
        <el-input v-model="subData.sort"
                  type="number"
                  class="w-300"
                  placeholder="请输入排序"></el-input>
      </el-form-item>

      <el-form-item label="判断标准配置：">
        <div style="display: flex;flex-direction: row">
          <p class="tv1">连续</p>
          <el-input v-model="subData.noDataControlDay"
                    clearable
                    style="width: 80px;margin-left: 10px;margin-right: 10px"
                    placeholder="必填"></el-input>
          <p class="tv1">天无数据</p>
        </div>
      </el-form-item>

      <el-form-item label="年龄段:">
        <el-radio-group v-model="chooseData.isAllAge">
          <el-radio :label="1">不限</el-radio>
          <el-radio :label="0">指定年龄段</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="chooseData.isAllAge===0" prop="dolThemeIndication.ageCondition">
        <el-select v-model="subData.dolThemeIndication.ageCondition"
                   class="w-300"
                   clearable
                   multiple
                   placeholder="请选择年龄段">
          <el-option v-for="item in ageDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别：">
        <el-select v-model="subData.dolThemeIndication.genderMode"
                   class="w-300"
                   clearable
                   placeholder="请选择性别">
          <el-option v-for="item in sexDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态：">
        <el-select v-model="subData.dolThemeIndication.bodyRiskMode"
                   class="w-300"
                   clearable
                   placeholder="请选择状态">
          <el-option v-for="item in statusDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="病症:">
        <el-radio-group v-model="chooseData.isAllDisease">
          <el-radio :label="1">不限</el-radio>
          <el-radio :label="0">指定病种</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="chooseData.isAllDisease===0">
        <el-tree
          :data="diseaseDataSource"
          show-checkbox
          node-key="id"
          :default-checked-keys="defaultCheckedKeys"
          :props="defaultProps"
          @check="handleCheckChange">
        </el-tree>
      </el-form-item>

    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="handleOption('save',null)">保 存
      </el-button>
    </p>

  </el-dialog>
</template>

<script>
import ElementTable from "../../../../base/ElementTable";

export default {
  name: "dialogProjectConfiguration",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: '新 增',

      subData: {
        name: '',
        dolThemeId: '',
        sort: '',
        noDataControlDay: '',
        dolThemeIndication: {
          ageCondition: [],
          genderMode: 0,
          bodyRiskMode: 0,
          themeIds: []
        },

      },
      chooseData: {
        isAllAge: 1,
        isAllDisease: 1,
      },
      classificationDataSources: [
        {
          name: '基础信息',
          id: 1
        },
        {
          name: '量表自测',
          id: 2
        },
        {
          name: '检测项',
          id: 3
        }
      ],
      themeDataSources: [],
      ageDataSource: [
        {
          id: '1',
          name: '婴儿期，0-6岁',
          disabled: false
        }, {
          id: '2',
          name: '少年儿童，7-12岁',
          disabled: false
        }, {
          id: '3',
          name: '青少年，13-17岁',
          disabled: false
        }, {
          id: '4',
          name: '青年，18-39岁',
          disabled: false
        }, {
          id: '5',
          name: '中年，40-64岁',
          disabled: false
        }, {
          id: '6',
          name: '老年，≥65岁',
          disabled: false
        },
      ],
      sexDataSource: [
        {
          id: 0,
          name: '不限'
        }, {
          id: 2,
          name: '女'
        }, {
          id: 1,
          name: '男'
        }
      ],
      statusDataSource: [
        {
          id: 0,
          name: '不限'
        }, {
          id: 1,
          name: '中风险以下'
        }, {
          id: 2,
          name: '中风险及以上'
        }
      ],
      columns: [
        {
          label: '类型',
          prop: 'titleContent',
          align: 'center'
        }, {
          label: '项目/量表/基础信息主题名称',
          prop: 'indexItems',
          align: 'center',
        }, {
          label: '指征',
          prop: 'sortNum',
          align: 'center',
          isSlot: true
        }, {
          label: '操作',
          prop: 'option',
          align: 'center',
          isSlot: true,
        }
      ],
      tableData: [],
      diseaseDataSource: [],
      defaultProps: {
        label: "themeName",
        children: "children"
      },
      defaultCheckedKeys: [],
      formModelRules: {
        name: [{required: true, message: '请输入常测项名称', trigger: 'blur'},],
        dolThemeId: [{required: true, message: '请选择主题', trigger: 'blur'},],
        sort: [{required: true, message: '请输入排序', trigger: 'blur'},],
        noDataControlDay: [{required: true, message: '请输入判断标准配置', trigger: 'blur'},],
        'dolThemeIndication.ageCondition': [{required: true, message: '请选择年龄段', trigger: 'blur'},],
        'dolThemeIndication.genderMode': [{required: true, message: '请选择性别', trigger: 'blur'},],
        'dolThemeIndication.bodyRiskMode': [{required: true, message: '请选择状态', trigger: 'blur'},],
      },
    }
  },
  created() {
    if (this.dataObj.id) {
      this.title = '编 辑'
      this.getConstantDetectDetail()
    } else {
      this.title = '新 增'
    }
    this.searchDolTheme()
    this.getTreeList()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false);
    },
    handleOption(op, index) {
      switch (op) {
        case 'save':
          this.$refs.formModelRef.validate(valid => {
            if (valid) {
              if (this.chooseData.isAllDisease === 0 && this.subData.dolThemeIndication.themeIds.length === 0) {
                this.$message.error('请选择病症')
              } else if (this.chooseData.isAllDisease === 1) {
                this.subData.dolThemeIndication.themeIds = []
              }
              if (this.chooseData.isAllAge === 1) {
                this.subData.dolThemeIndication.ageCondition = []
                this.subData.dolThemeIndication.ageCondition.push(0)
              }
              let mydata = JSON.parse(JSON.stringify(this.subData))

              mydata.dolThemeIndication.ageCondition = mydata.dolThemeIndication.ageCondition.toString()
              this.post(this.api.healthMap.updateDolConstant, mydata).then(res => {
                if (res.data.code === '200') {
                  this.$message.success('保存成功')
                  this.modalClose()
                  this.$parent.getList()
                }
              })
            }
          })
          break
      }

    },

    //查询病种
    getTreeList() {
      this.post(this.api.promote.getTreeList, {
        pageIndex: 0,
        pageSize: 100,
        searchNoChronic: 1
      }).then(res => {
        if (res.data.code === '200') {
          this.diseaseDataSource = res.data.object;
        }
      })
    },

    //查询主题
    searchDolTheme() {
      this.post(this.api.healthMap.searchDolTheme, {
        useType: 3
      }).then((res) => {
        if (res.data.code === '200') {
          this.themeDataSources = res.data.object
        }
      })
    },

    //查询详情
    getConstantDetectDetail() {
      this.post(this.api.healthMap.getDolConstantDetail, {
        id: this.dataObj.id
      }).then(res => {
        if (res.data.code === '200') {

          this.subData = Object.assign(this.subData, res.data.object);
          if (this.subData.dolThemeIndication.ageCondition === '0') {
            //不限年龄
            this.chooseData.isAllAge = 1
          } else {
            this.chooseData.isAllAge = 0
            this.subData.dolThemeIndication.ageCondition = res.data.object.dolThemeIndication.ageCondition.split(',')
          }
          if (this.subData.dolThemeIndication.themeIds.length === 0) {
            this.chooseData.isAllDisease = 1
          } else {
            this.chooseData.isAllDisease = 0
            this.defaultCheckedKeys = res.data.object.dolThemeIndication.themeIds
          }
        }
      })
    },
    handleCheckChange(obj, selectObj) {
      let checkedNodes = selectObj.checkedKeys;
      checkedNodes = checkedNodes.filter(item => item)

      this.subData.dolThemeIndication.themeIds = checkedNodes
    },
  },
  components: {
    'element-table': ElementTable,
  }
}
</script>

<style scoped>

</style>
